package com.example.iotest.nio;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Slf4j
public class NIOTest {
    public static void main(String[] args) throws IOException, InterruptedException {
//        readSomething();
//        copy();
//        test2();
//        Thread.sleep(10000);
        test1();
    }

    public static void readSomething() throws IOException {
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        FileInputStream fileInputStream = new FileInputStream("src/main/resources/files/1.txt");
        FileChannel channel = fileInputStream.getChannel();
        channel.read(buffer);
        buffer.flip();
        char b = buffer.getChar();
        System.out.println(b);
    }

    public static void copy() throws IOException {
        File fileIn = new File("src/main/resources/files/1.txt");
        long length = fileIn.length();
        System.out.println(length);
//        FileInputStream fileInputStream = new FileInputStream(fileIn);
        File fileOut = new File("src/main/resources/files/2.txt");
//        FileOutputStream fileOutputStream = new FileOutputStream(fileOut);
//        FileChannel in = fileInputStream.getChannel();
//        FileChannel to = fileOutputStream.getChannel();
//        in.transferTo(0,to.size(),to);
        Files.copy(Paths.get("src/main/resources/files/11.jpg"),
                Paths.get("src/main/resources/files/22.jpg"),
                StandardCopyOption.REPLACE_EXISTING);
//        in.close();
//        to.close();
//        fileInputStream.close();
//        fileOutputStream.close();
    }

    public static void test1() throws IOException {
        SocketChannel socketChannel = SocketChannel.open();
        boolean connect = socketChannel.connect(new InetSocketAddress("192.168.1.28", 8000));
        ByteBuffer wrap = ByteBuffer.wrap("hello".getBytes());
        wrap.flip();
        socketChannel.write(wrap);
        wrap.clear();
        socketChannel.close();

    }

    public static void test2() throws IOException {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.configureBlocking(false);
        serverSocketChannel.bind(new InetSocketAddress(8000), 100);
        Selector selector = Selector.open();
        SelectionKey register = serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
//        log.info(register.selector().toString());
        for (; ; ) {
            int select = selector.select(3000);
            if (select == 0) {
                log.info("===");
                continue;
            }
            Set<SelectionKey> selectionKeys = selector.selectedKeys();
            selectionKeys.forEach((key) -> {
                log.info("Key: " + key);
                if (key.isAcceptable()) {
                    log.info("连接上了");
                }
                if (key.isReadable()) {
                    log.info("连接上了");
                }
                if (key.isConnectable()) {
                    log.info("连接上了");
                }
            });
        }
    }
}
